package String;

import org.junit.Test;

/**
 * @Classname String.JZ67把字符转为整数
 * @Description TODO
 * @Date 2023/3/8 21:45
 * @Created by xjl
 */
public class JZ67把字符转为整数 {
    public int strToInt(String str) {
        char[] c = str.trim().toCharArray();
        if (c.length == 0) {
            return 0;
        }
        int res = 0, bndry = Integer.MAX_VALUE / 10;
        int i = 0, sign = 1;
        if (c[0] == '-') {
            sign = -1;
            i++;
        } else if (c[0] == '+') {
            i++;
        }
        for (int j = i; j < c.length; j++) {
            if (c[j] < '0' || c[j] > '9') {
                break;
            }
            if (res > bndry || res == bndry && c[j] > '7') {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            res = res * 10 + (c[j] - '0');
        }
        return sign * res;
    }

    @Test
    public void test(){
        int i = strToInt("         -42");
        System.out.println(i);

    }
}
